package ltd.starriddle.smart4j.chapter2.controller;

import ltd.starriddle.smart4j.chapter2.model.Customer;
import ltd.starriddle.smart4j.chapter2.service.CustomerService;
import ltd.starriddle.smart4j.chapter2.util.CastUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 编辑客户
 *
 * @author chen
 */
@WebServlet("/customer_edit")
public class CustomerEditServlet extends HttpServlet {

    private static final Logger LOGGER = LoggerFactory.getLogger(CustomerEditServlet.class);

    private CustomerService customerService;

    @Override
    public void init() throws ServletException {
        customerService = new CustomerService();
    }

    /**
     * 进入 编辑客户 页面
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        long id = CastUtil.castLong(req.getParameter("id"));
        LOGGER.info("编辑客户 id={}", id);

        Customer customer = customerService.getCustomer(id);
        req.setAttribute("customer", customer);
        LOGGER.info("编辑客户 customer={}", customer);

        req.getRequestDispatcher("/WEB-INF/view/customer_edit.jsp").forward(req, resp);
    }

    /**
     * 处理 编辑客户 请求
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        long id = CastUtil.castLong(req.getParameter("id"));
        String name = req.getParameter("name").trim();
        String contact = req.getParameter("contact").trim();
        String phone = req.getParameter("phone").trim();
        String email = req.getParameter("email").trim();
        String remark = req.getParameter("remark").trim();
        Customer customer = customerService.getCustomer(id);
        Map<String, Object> fieldMap = new HashMap<>();
        fieldMap.put("name", name);
        fieldMap.put("contact", contact);
        fieldMap.put("phone", phone);
        fieldMap.put("email", email);
        fieldMap.put("remark", remark);
        LOGGER.info("req 编辑客户 id={} fieldMap={} customer={}", id, fieldMap, customer);

        boolean result = false;
        String msg = "修改失败";
        if (customer == null) {
            msg = "客户不存在";
        } else if (StringUtils.isAnyBlank(name, contact)) {
            msg = "名称 和 联系人 不能为空";
        } else  {
            result = customerService.updateCustomer(id, fieldMap);
            if (result) {
                result = true;
                msg = "修改成功";
                customer = customerService.getCustomer(id);
            }
        }
        req.setAttribute("customer", customer);
        req.setAttribute("result", result);
        req.setAttribute("msg", msg);
        LOGGER.info("resp 编辑客户 result={} msg={} customer={}", result, msg, customer);

        req.getRequestDispatcher("/WEB-INF/view/customer_edit.jsp").forward(req, resp);
    }
}
